#include "board.h"
#include "drv_serial.h"

void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */


void cpu_init(void)
{
    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
    LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);

    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

    NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),15, 0));
}


/***************   serial config   *************/

struct stm32f4xx_gpio_s      stm32f4xx_uart1_txd  = STM32F4XX_UART1_TXD_PIN1;
struct stm32f4xx_gpio_s      stm32f4xx_uart1_rxd  = STM32F4XX_UART1_RXD_PIN1;
struct stm32f4xx_uart_data_s stm32f4xx_uart1_data = STM32F4XX_UART1_DATA;
struct lt_serial_s           stm32f4xx_serial1;

struct stm32f4xx_dma_data_s  stm32f4xx_uart1_dma_tx = STM32F4XX_UART1_TX_DMA27;
struct stm32f4xx_dma_data_s  stm32f4xx_uart1_dma_rx = STM32F4XX_UART1_RX_DMA22;


struct stm32f4xx_gpio_s      stm32f4xx_uart4_txd  = STM32F4XX_UART4_TXD_PIN2;
struct stm32f4xx_gpio_s      stm32f4xx_uart4_rxd  = STM32F4XX_UART4_RXD_PIN2;
struct stm32f4xx_uart_data_s stm32f4xx_uart4_data = STM32F4XX_UART4_DATA;
struct lt_serial_s           stm32f4xx_serial4;


struct stm32f4xx_gpio_s      stm32f4xx_uart6_txd  = STM32F4XX_UART6_TXD_PIN2;
struct stm32f4xx_gpio_s      stm32f4xx_uart6_rxd  = STM32F4XX_UART6_RXD_PIN2;
struct stm32f4xx_uart_data_s stm32f4xx_uart6_data = STM32F4XX_UART6_DATA;
struct lt_serial_s           stm32f4xx_serial6;

struct stm32f4xx_dma_data_s  stm32f4xx_uart6_dma_tx = STM32F4XX_UART6_TX_DMA26;
struct stm32f4xx_dma_data_s  stm32f4xx_uart6_dma_rx = STM32F4XX_UART6_RX_DMA21;

int32_t  stm32f4xx_pwm_register(void);
int32_t  flash_dev_register(void);
int32_t  stm32f4xx_rgbled_register(void);
int      stm32f4xx_gpio_register(void);

void board_initialize(void)
{
    cpu_init();
    lt_dev_init();

    stm32f4xx_uart1_data.txd = &stm32f4xx_uart1_txd;
    stm32f4xx_uart1_data.rxd = &stm32f4xx_uart1_rxd;
    stm32f4_serial_register("ttyS1", &stm32f4xx_serial1, &stm32f4xx_uart1_data, LT_DEVICE_FLAG_INT_RX | LT_DEVICE_FLAG_INT_TX | O_RDWR);
    
    stm32f4xx_uart4_data.txd = &stm32f4xx_uart4_txd;
    stm32f4xx_uart4_data.rxd = &stm32f4xx_uart4_rxd;
    //stm32f4_serial_register("ttyS4", &stm32f4xx_serial4, &stm32f4xx_uart4_data, LT_DEVICE_FLAG_INT_RX | LT_DEVICE_FLAG_INT_TX | O_RDWR);

    stm32f4xx_uart6_data.txd    = &stm32f4xx_uart6_txd;
    stm32f4xx_uart6_data.rxd    = &stm32f4xx_uart6_rxd;
    stm32f4_serial_register("ttyS6", &stm32f4xx_serial6, &stm32f4xx_uart6_data, LT_DEVICE_FLAG_INT_RX | LT_DEVICE_FLAG_INT_TX | O_RDWR);
    
    stm32f4xx_gpio_register();
    flash_dev_register();
}

#define APP_LOAD_ADDRESS 0x08008000


void board_deinit(void)
{
  LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
  LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
  LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
  LL_APB2_GRP1_DisableClock(LL_APB2_GRP1_PERIPH_USART1);
  LL_APB2_GRP1_DisableClock(LL_APB2_GRP1_PERIPH_USART6);
  
  LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOA);
  LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOB);
  LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOC);
  LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOA);
  LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOB);
  LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOC);

  // 复位 USART 外设
  LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART1);
  LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART1);
  LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART6);
  LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART6);
  
  WWDG->CR = 0x00;
  
  __disable_irq();
}

static void board_do_jump(uint32_t stacktop, uint32_t entrypoint)
{
       asm volatile(
               "msr msp, %0    \n"
               "bx     %1      \n"
               : : "r"(stacktop), "r"(entrypoint) :);
}

int board_jump_to_app(void)
{
  const uint32_t *app_base = (const uint32_t *)APP_LOAD_ADDRESS;
  const uint32_t *vec_base = (const uint32_t *)app_base;

  if (app_base[0] == 0xffffffff) {
    return -1;
  }

  board_deinit();

  SCB->VTOR = vec_base;
  board_do_jump(app_base[0], app_base[1]);
  return 0;
}
